package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.link.utils.AddressHelper;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.jicai.view.fragment.JiCaiXuFangFaQiFragment;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import retrofit2.Call;
/**
 * 首页需方发起Fragment
 */
public class JiCaiXuFangFaQiFraModel extends ViewModel {

    private  CommonPopEntity mAll;
    private JiCaiXuFangFaQiFragment fragment;
    private int page = 1;
    private Map map;

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
            fragment.closeRecyclerRefreshing();
        }
    }
    /**
     * 需方发起列表
     */
    public void getJiCaiList(JiCaiHomeListRequestBean map, boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.setCurrent(page);
        map.setLimit(10);
        showOrHideDialog(true);
        JiCaiBaseApi.getApi().getJiCaiLists(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<CentralizedMiningEntity>>> data) {
                showOrHideDialog(false);
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    fragment.setJiCaiList(data.getData().getList(), isLoadMore);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void getH5Url(String id) {
        showOrHideDialog(true);
        JiCaiBaseApi.getApi().getUrlSetting("collection").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                showOrHideDialog(false);
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(fragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = data.getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + token;
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    public void getMethodsList(int type, boolean isShowLeftDialog){
        List<CommonPopEntity> datas = new ArrayList<>();
        datas.add(new CommonPopEntity("", "全部"));
        datas.add(new CommonPopEntity("1", "即报即采"));
        datas.add(new CommonPopEntity("2", "汇集采购"));
        if (isShowLeftDialog) {
            fragment.showLeftDialog(type, datas);
        } else {
            fragment.setChooseItemData(type, datas);
        }
    }

    //报价客户
    public void getOfferList(int type,boolean isShowLeftDialog) {
        List<CommonPopEntity> datas = new ArrayList<>();
        datas.add(new CommonPopEntity("", "全部"));
        datas.add(new CommonPopEntity("1", "客户报价"));
        datas.add(new CommonPopEntity("2", "市场报价"));
        datas.add(new CommonPopEntity("3", "成员报价"));
        if (isShowLeftDialog) {
            fragment.showLeftDialog(type, datas);
        } else {
            fragment.setChooseItemData(type, datas);
        }
    }


    //集采状态
    public void groupBuyClassList(int type,boolean isShowLeftDialog) {
        showOrHideDialog(true);
        JiCaiBaseApi.getApi().getStatus().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().get(0).setNo(""); // 全部的key值为-1，
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    //二级分类
    public void getTenderGoodsCategoryAllList(int type, String no,String  index, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        Call<BaseEntity<List<CommonPopEntity>>> call = null;
        if ("1".equals(index)) { // 商品材料
            call = JiCaiBaseApi.getApi().getSecondCategory(no, "1");
        } else if ("2".equals(index)) { // 机械租赁
            call = JiCaiBaseApi.getApi().getSecondCategory(no, "2");

        } else if ("3".equals(index)) { // 设备租赁
            call = JiCaiBaseApi.getApi().getSecondCategory(no, "3");
        }
        if (call == null) {
            return;
        }
        call.enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    data.getData().add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(int type) {
        showOrHideDialog(true);
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                showOrHideDialog(false);
                if (data != null && data.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    fragment.setChooseItemData(type, entities);
                    fragment.setAddressData(data);
                }
            }
        });
    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, int type, boolean isShowLeftDialog) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new CommonPopEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, entities);
                    } else {
                        fragment.setChooseItemData(type, entities);
                    }
                }
            }
        }
    }

    //加盟客户
    public void getJoinTypeList(int type,boolean isShowLeftDialog) {
        List<CommonPopEntity> datas = new ArrayList<>();
        datas.add(new CommonPopEntity("", "全部"));
        datas.add(new CommonPopEntity("1", "客户加盟"));
        datas.add(new CommonPopEntity("2", "市场加盟"));
        datas.add(new CommonPopEntity("3", "成员加盟"));
        if (isShowLeftDialog) {
            fragment.showLeftDialog(type, datas);
        } else {
            fragment.setChooseItemData(type, datas);
        }
    }

    public void setActivity(JiCaiXuFangFaQiFragment jiCaiXuFangFaQiFragment) {
        this.fragment = jiCaiXuFangFaQiFragment;
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }
}
